package com.ruoyi.web.controller.shop;

import cn.hutool.core.lang.Validator;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ImsVehMerUnit;
import com.ruoyi.system.service.IImsVehMerUnitService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商户单位信息管理
 * 
 * @author ruoyi
 * @date 2024-07-10
 */
@RestController
@RequestMapping("/merunit/vehmerunit")
public class ImsVehMerUnitController extends BaseController
{
    @Autowired
    private IImsVehMerUnitService imsVehCusUnitService;

    /**
     * 查询商户单位列表
     */
    //@PreAuthorize("@ss.hasPermi('merunit:vehmerunit:list')")
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(ImsVehMerUnit imsVehCusUnit)
    {
        startPage();
        List<ImsVehMerUnit> list = imsVehCusUnitService.selectImsVehCusUnitList(imsVehCusUnit);
        return getDataTable(list);
    }

    /**
     * 导出商户单位列表
     */
    @PreAuthorize("@ss.hasPermi('merunit:vehmerunit:export')")
    @Log(title = "商户单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImsVehMerUnit imsVehCusUnit)
    {
        List<ImsVehMerUnit> list = imsVehCusUnitService.selectImsVehCusUnitList(imsVehCusUnit);
        ExcelUtil<ImsVehMerUnit> util = new ExcelUtil<ImsVehMerUnit>(ImsVehMerUnit.class);
        util.exportExcel(response, list, "商户单位数据");
    }

    /**
     * 获取商户单位详细信息
     */
    //@PreAuthorize("@ss.hasPermi('merunit:vehmerunit:query')")
    @GetMapping(value = "/{id}")
    @Anonymous
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(imsVehCusUnitService.selectImsVehCusUnitById(id));
    }


    @GetMapping("/getDeptId/{deptId}")
    @Anonymous
    public AjaxResult IImsVehMerUnitService(@PathVariable("deptId") Long deptId){
        return success(imsVehCusUnitService.selectImsVehCusUnitByDeptId(deptId));
    }

    /**
     * 新增商户单位
     */
    //@PreAuthorize("@ss.hasPermi('merunit:vehmerunit:add')")
    @Log(title = "商户单位", businessType = BusinessType.INSERT)
    @PostMapping
    @Anonymous
    public AjaxResult add(@RequestBody ImsVehMerUnit imsVehCusUnit)
    {
        AjaxResult ajax=AjaxResult.success();
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getPersonTel())) {
            if (!Validator.isMobile(imsVehCusUnit.getPersonTel())) {
                return error("手机号不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getPersonEmail())) {
            if (!Validator.isEmail(imsVehCusUnit.getPersonEmail())) {
                return error("邮箱不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getPersonIdCard())) {
            if (!Validator.isCitizenId(imsVehCusUnit.getPersonIdCard())) {
                return error("身份证号码不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getComContactIdCard())) {
            if (!Validator.isCitizenId(imsVehCusUnit.getComContactIdCard())) {
                return error("法人身份证号码不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getComEmail())) {
            if (!Validator.isEmail(imsVehCusUnit.getComEmail())) {
                return error("企业邮箱不正确！");
            }
        }
        imsVehCusUnit.setAddTime(DateUtils.getNowDate());
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        if (loginUser != null) {
            imsVehCusUnit.setDeptId(loginUser.getDeptId());
        }
        imsVehCusUnitService.insertImsVehCusUnit(imsVehCusUnit);
        ajax.put("cusUnitId",imsVehCusUnit.getId());
        return ajax;
    }

    /**
     * 修改商户单位
     */
    //@PreAuthorize("@ss.hasPermi('merunit:vehmerunit:edit')")
    @Log(title = "商户单位", businessType = BusinessType.UPDATE)
    @PutMapping
    @Anonymous
    public AjaxResult edit(@RequestBody ImsVehMerUnit imsVehCusUnit)
    {
        // 判断是否为个人身份证
        if (ObjectUtils.isNotEmpty(imsVehCusUnit.getUnitType()) && imsVehCusUnit.getUnitType() == 1) {
            if(ObjectUtils.isNotEmpty(imsVehCusUnit.getPersonIdCard())) {
                if (!Validator.isCitizenId(imsVehCusUnit.getPersonIdCard())) {
                    return error("身份证号码不正确！");
                }
            }
        }else{
            if(ObjectUtils.isNotEmpty(imsVehCusUnit.getComContactIdCard())) {
                if (!Validator.isCitizenId(imsVehCusUnit.getComContactIdCard())) {
                    return error("法人身份证号码不正确！");
                }
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehCusUnit.getPersonTel())) {
            if (!Validator.isMobile(imsVehCusUnit.getPersonTel())) {
                return error("手机号不正确！");
            }
        }
        return toAjax(imsVehCusUnitService.updateImsVehCusUnit(imsVehCusUnit));
    }

    /**
     * 删除商户单位
     */
    //@PreAuthorize("@ss.hasPermi('cusunit:vehcusunit:remove')")
    @Log(title = "商户单位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @Anonymous
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imsVehCusUnitService.deleteImsVehCusUnitByIds(ids));
    }
}
